//
//  HistoryListViewController.swift
//  BookStore2.0
//
//  Created by hw on 2022/6/2.
//

import UIKit

class HistoryListViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
    
    var refresh=UIRefreshControl()
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if OrderList.sharedinstance.orderlist != nil{
            return OrderList.sharedinstance.orderlist!.count
        }
        else {
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "historyordercell", for: indexPath) as! HistoryOrderTableViewCell
        let orderlist=OrderList.sharedinstance.orderlist
        
        if orderlist != nil{
            cell.ID.text="\((orderlist![indexPath.row]["id"] as! NSNumber).intValue)"
            cell.booknum.text="\((orderlist![indexPath.row]["booknumSum"] as! NSNumber).intValue)"
            cell.totalPrice.text="\((orderlist![indexPath.row]["moneySum"] as! NSNumber).floatValue)"
            cell.orderTime.text=orderlist![indexPath.row]["orderTime"] as? String
        }
        return cell
    }
    
    @IBOutlet weak var tableView2: UITableView!
    
    var IDv:Int?
    var BookNumv:Int?
    var totalPricev:Float?
    var orderTimev:String?
    
    @objc func reload()  {
        self.tableView2.reloadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        let xib=UINib(nibName: "HistoryOrderTableViewCell", bundle: nil)
        tableView2.register(xib, forCellReuseIdentifier: "historyordercell")
        tableView2.rowHeight=130
        //下拉刷新
        refresh.addTarget(self, action:#selector(reload), for:.valueChanged)
        refresh.attributedTitle=NSAttributedString(string: "正在刷新")
        tableView2.addSubview(refresh)
        // Do any additional setup after loading the view.
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        IDv=(OrderList.sharedinstance.orderlist![indexPath.row]["id"] as! NSNumber).intValue
        BookNumv=(OrderList.sharedinstance.orderlist![indexPath.row]["booknumSum"] as! NSNumber).intValue
        totalPricev=(OrderList.sharedinstance.orderlist![indexPath.row]["moneySum"] as! NSNumber).floatValue
        orderTimev=OrderList.sharedinstance.orderlist![indexPath.row]["orderTime"] as? String
        performSegue(withIdentifier: "showdetail", sender: self)
        
    }
    override func viewWillAppear(_ animated: Bool){
        tableView2.reloadData()
        OrderList.sharedinstance.orderlist=DB1.selectAllOrder()
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       
        let nextVC=segue.destination as! OrderDetailViewController
        nextVC.orderid=IDv
        nextVC.booknum=BookNumv
        nextVC.totalprice=totalPricev
        nextVC.ordertime=orderTimev
    }    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
}
